{include file="public/header"/}
</head>
<body>
<div class="layui-fluid" id="LAY-component-layer-list">
    <div class="layui-card">
        <div class="layui-card-body table-tool-mini">
            <div class="layui-form toolbar">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <a href="{:url('/type/index')}" class="layui-btn layui-btn-primary input-btn-back"><i class="layui-icon layui-icon-left"></i></a>
                        <button class="layui-btn icon-btn" data-url="{:url('/field/channel_add',['channel_id'=>$channel_id])}" data-type="channel_add"><i class="layui-icon">&#xe654;</i>新增栏目字段</button>
                    </div>
                </div>
            </div>
            <table id="tablelist" lay-filter="tablelist"></table>
        </div>
    </div>
</div>
<script type="text/html" id="oper-col">
{{# if(d.ifcontrol == 1){ }}
 <a class="layui-btn layui-btn-primary layui-btn-sm layui-btn-disabled">编辑</a>
{{# }else{ }}
<a class="layui-btn layui-btn-primary layui-btn-sm" data-channelid="{{d.channel_id}}" data-id="{{d.id}}" data-type="channel_edit">编辑</a>
{{# } }}

{{# if(d.ifsystem == 1 || d.ifcontrol == 1){ }}
 <a class="layui-btn layui-btn-danger layui-btn-sm layui-btn-disabled">删除</a>
{{# }else{ }}
<a class="layui-btn layui-btn-danger layui-btn-sm" data-channelid="{{d.channel_id}}" data-id="{{d.id}}" data-type="channel_del">删除</a>
{{# } }}
</script>
{include file="public/footer"/}
<script>
    layui.use(['layer', 'form', 'table'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;

        // 渲染表格
        var insTb = table.render({
            elem: '#tablelist',
            url: "{:url('/field/channel_index',['channel_id'=>$channel_id])}",
            page: true,
            toolbar: false,
            cellMinWidth: 100,
            cols: [[
                {field:'id',title:'ID',width:80},
                {field: 'title',title: '字段标题'},
                {field: 'name',title: '字段名称',width:180},{ templet: function (d) {
                    if (d.dtypename) {
                       var html1 = d.dtypename;
                    } else {
                       var html1 = '数据错误';
                    }
                    return html1;
                },title: '字段类型',width:150},{templet: function (d) {
                    if (d.ifsystem == 1 && d.ifmain == 1) {
                       var html1 = '系统';
                    } else {
                       var html1 = '自定义';
                    }
                    return html1;
                },title: '字段分类',width:100,align: 'center'},

                {field: 'status', templet: function (d) {
                    if (d.ifcontrol == 1) {
                       var html2 = '——';
                    } else {
                        if(d.status == 1){
                           var html2 = '<input type="checkbox" lay-skin="switch" lay-filter="ckStateUser" data-id="'+d.id+'" lay-text="是|否" checked>';
                        }else{
                           var html2 = '<input type="checkbox" lay-skin="switch" lay-filter="ckStateUser" data-id="'+d.id+'" lay-text="是|否">';
                        }
                    }
                    return html2;
                },width: 100, title: '状态',align: 'center'},
                {field: 'update_time', title: '更新时间',width:200},
                {title: '排序',align: 'center',templet: function (d) {
                    if(d.sort_order){
                       var html = "<input type='text' size='4' data-id='"+d.id+"' value='"+d.sort_order+"' id='sort_order' class='layui-input' style='height:28px;'/>";
                    }else{
                       var html = ""+d.sort_order+""; 
                    }
                    return html;
                }, width: 80},
                {align: 'center', toolbar: '#oper-col', title: '操作', width: 150}
            ]]
        });

        $(document).on("click","#LAY-component-layer-list .layui-btn",function(){
          var type = $(this).data('type');
          active[type] && active[type].call(this);
        });

        //触发事件
        var active = {
            channel_add: function(){
                var iframes = layer.open({
                    type: 2,
                    title: '新增字段',
                    fixed: true,
                    shadeClose: false,
                    shade: 0.3,
                    content: $(this).data('url')
                });
                layer.full(iframes);
            },
            channel_edit: function(){
                var obj = this;
                var id = $(obj).attr('data-id');
                var channelid = $(obj).attr('data-channelid');
                var iframes = layer.open({
                    type: 2,
                    title: '编辑字段',
                    fixed: true,
                    shadeClose: false,
                    shade: 0.3,
                    content: '{:url("/field/channel_edit")}?channel_id='+channelid+'&id='+id
                });
                layer.full(iframes);
            },
            channel_del: function(){
                var obj = this;
                var id = $(obj).attr('data-id');
                var channelid = $(obj).attr('data-channelid');
                layer.msg('数据将无法恢复，确认删除？', {
                    btnAlign: 'c',
                    time: 0,
                    btn: ['确定', '取消'],
                    yes: function(index, layero){
                      layer.close(index);
                      layer.msg('正在处理请勿刷新页面', {icon: 16,shade: 0.01,time:3600000});
                      $.ajax({
                            type : 'post',
                            url : '{:url("/field/channel_del")}?channel_id='+channelid,
                            data : {del_id:id},
                            dataType : 'json',
                            success : function(res){
                              layer.closeAll('loading');
                              if(res.status){
                                layer.msg(res.msg, {icon: 1, time:1000}, function(){
                                  window.location.reload();
                                });
                              }else{
                                layer.msg(res.msg, {icon: 2, time: 1000});
                              }
                            },error: function(e){
                              layer.closeAll('loading');
                              layer.msg("未知错误，操作中断！", {icon: 2, time: 1000});
                            }
                      })
                    }
                });
            }
        };

        // 监听排序
        $(document).on("change","#LAY-component-layer-list #sort_order",function(){
            var dataid = $(this).attr('data-id');
            var pcfurl = '{:url("/ajax/ajax_changeTableVal")}';
            changeTableVal('channelfield','id',dataid,'sort_order',this,pcfurl,'type/index');
            self.window.location.reload();
        })

        //开启关闭
        form.on('switch(ckStateUser)', function(data){
            var openstatus = 3; 
            if (data.elem.checked) {
              this.value = 1;
            } else {
              this.value = 0;
            }
            var dataid = $(this).attr('data-id');
            var pcfurl = '{:url("/ajax/ajax_changeTableVal")}';
            changeTableVal('channelfield','id',dataid,'status',this,pcfurl,'type/index',openstatus);
        });

    });
</script>
</body>
</html>